package com.cardbaobao.cardbabyclient.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.callback.ItemClickedCallback;
import com.cardbaobao.cardbabyclient.controller.CommonJumpController;
import com.cardbaobao.cardbabyclient.model.Banner;
import com.cardbaobao.cardbabyclient.model.UpperQuota;
import com.cardbaobao.cardbabyclient.model.result.BannerResult;
import com.cardbaobao.cardbabyclient.model.result.UpperQuotaResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.activity.web.EventWebViewActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.UpperQuotaAdapter;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;
import com.stx.xhb.xbanner.XBanner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/12 14:33.
 * 信用卡提额
 */

public class UpperQuotaActivity extends BaseActivity{
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.xb_banner)
    XBanner xBanner;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_upper_quota;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mCurrentPageId= Self.Page.app_mention_the_amount_of;

        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //banner宽高比750：280
        ViewGroup.LayoutParams params=xBanner.getLayoutParams();
        final int screenWidth= ScreenUtil.getScreenWidth();
        params.width=screenWidth;
        params.height=(int) (28*screenWidth/75.0f);
        xBanner.setLayoutParams(params);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        loadBanner();
        loadUpperQuotaList();
    }

    private void loadBanner(){
        showProgressDialog(false);
        Api.getBannerData(Constant.GLB0334).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                BannerResult result=JsonUtil.json2Object(s,BannerResult.class);
                if(result.getCode()==Constant.SUC_CODE){
                    Banner banner=result.getData();
                    if(banner!=null){
                        final List<Banner.CarouselFigureListBean> data=
                                banner.getCarousel_figure_list();
                        xBanner.stopAutoPlay();
                        if(data!=null&&data.size()>0){
                            xBanner.setData(data,null);
                            xBanner.setmAdapter(new XBanner.XBannerAdapter() {
                                @Override
                                public void loadBanner(XBanner xBanner, Object o, View view, int i) {
                                    Banner.CarouselFigureListBean bean= (Banner.CarouselFigureListBean) o;
                                    ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL+bean.getImage(),
                                            (ImageView) view);
                                }
                            });
                            xBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                                @Override
                                public void onItemClick(XBanner xBanner, int i) {
                                    CommonJumpController c=new CommonJumpController(mContext);
                                    Map<String,Object> extras=new HashMap<>();
                                    extras.put(CommonJumpController.FROM_PAGE,
                                            "提额");
                                    c.bannerJump(data.get(i),extras);
                                }
                            });
                            xBanner.startAutoPlay();
                        }else{
                            xBanner.setVisibility(View.GONE);
                        }
                    }
                }else{
                    ToastUtil.showToast(result.getMsg());
                    xBanner.stopAutoPlay();
                    xBanner.setVisibility(View.GONE);
                }
            }
        });
    }

    private void loadUpperQuotaList(){
        showProgressDialog(false);
        Api.upperQuotaList().subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.
                        getApplicationContext().
                        getString(R.string.string_request_server_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                UpperQuotaResult result= JsonUtil.json2Object(s,UpperQuotaResult.class);
                if(result.getCode()== Constant.SUC_CODE){
                    UpperQuota quota=result.getData();
                    if(quota!=null){
                        List<UpperQuota.Bean> data=quota.getMention();
                        if(data!=null&&data.size()>0){
                            GridLayoutManager layoutManager=
                                    new GridLayoutManager(mContext,4);
                            recyclerView.setLayoutManager(layoutManager);
                            UpperQuotaAdapter adapter=new UpperQuotaAdapter(data);
                            adapter.setItemClickedCallback(new ItemClickedCallback<UpperQuota.Bean>() {
                                @Override
                                public void onItemClicked(UpperQuota.Bean bean) {
                                    initIntent();
                                    mIntent.setClass(mContext,EventWebViewActivity.class);
                                    mIntent.putExtra(EventWebViewActivity.WEB_TITLE_S,
                                            bean.getName());
                                    mIntent.putExtra(EventWebViewActivity.WEB_URL_S,
                                            URLUtil.UPPER_QUOTA_DETAIL+bean.getId());
                                    mIntent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
                                    mIntent.putExtra(EventWebViewActivity.WEB_TYPE_I,
                                            43);
                                    startActivity(mIntent);
                                }
                            });
                            recyclerView.setAdapter(adapter);
                        }
                    }
                }else{
                    ToastUtil.showToast(result.getMsg());
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        GoogleAnalyticsUtil.setScreenName("信用卡提额");
        xBanner.startAutoPlay();
    }

    @Override
    protected void onPause() {
        super.onPause();
        xBanner.stopAutoPlay();
    }
}
